﻿/***************************************************
 * File name: ListHelper.cs 
 * Author: Team A
 * Change Log: Please see SVN repository: http://test-project-drew1.googlecode.com/svn/trunk/
 * Date: December 2013
 * Description: See <summary> tab below
 * *************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WSCeCommerce.Domain;
using WSCeCommerce.Helpers;

namespace WSCeCommerceWeb.Helpers
{
    /// <summary>
    /// Helper class to create List objects to use in the UI
    /// </summary>
    public class ListHelper
    {

        public const int SEARCH_ALL_CATEGORIES = int.MinValue;

        private static WSCeCommerceDBContext _context = new WSCeCommerceDBContext();

        /// <summary>
        /// Create a list of SelectionItem objects for all the categories in the dabase
        /// </summary>
        /// <returns>A list of categories</returns>
        public static List<SelectListItem> GetCategories(bool addAll,int selectCategory = SEARCH_ALL_CATEGORIES)
        {

            //Get the categories from the database
            List<Category> categories = _context.Categories.ToList();

            List<SelectListItem> result = new List<SelectListItem>();

            if (addAll)
            {
                result.Add(new SelectListItem() { Value =SEARCH_ALL_CATEGORIES.ToString()
                    , Text="All Categories"
                    , Selected= selectCategory == SEARCH_ALL_CATEGORIES });
            }

            // Foreach category create a SelectListItem object and add it to the result.
            foreach (Category cat in categories)
            {
                SelectListItem item = new SelectListItem()
                {
                    Value = cat.CategoryId.ToString(),
                    Text = cat.CategoryName,
                    Selected = selectCategory == cat.CategoryId
                };
                result.Add(item);
            }

            return result;
        }


        public static List<SelectListItem> GetCountries()
        {

            List<SelectListItem> result = new List<SelectListItem>();

            result.Add(new SelectListItem() { Text = "Canada" });
            result.Add(new SelectListItem() { Text = "USA" });
            result.Add(new SelectListItem() { Text = "Netherlands" });
            result.Add(new SelectListItem() { Text = "China" });
            result.Add(new SelectListItem() { Text = "Japan" });
            result.Add(new SelectListItem() { Text = "Australia" });
            result.Add(new SelectListItem() { Text = "Mexico" });
            result.Add(new SelectListItem() { Text = "Isle of Man" });

            return result;
        }


        /// <summary>
        /// Get a list of address types
        /// </summary>
        /// <returns></returns>
        public static List<SelectListItem> GetAddressTypes()
        {
            return EnumToList(typeof(AddressType));
        }

        public static List<SelectListItem> GetCreditCardTypes()
        {
            return EnumToList(typeof(CreditCardTypes));
         }

        private static  List<SelectListItem> EnumToList(Type type){
      
            List<SelectListItem> result = new List<SelectListItem>();

            foreach (String name in Enum.GetNames(type))
            {
                result.Add(new SelectListItem()
                {
                    Text = name,
                    Value = Enum.Parse(type,name).ToString()
                });
            }
            return result;
        }
    }
}